package org.pictual.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.Charset;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

import org.apache.log4j.Logger;
import org.pictual.controller.GoogleSearchImg;

public class GetPronun extends HTMLEditorKit.ParserCallback{

	private String phonetic;
	private boolean hasPhonetic;
	int pos;
	private String idioma;
	private String word;
	
	protected static Logger log= Logger.getLogger(GetPronun.class);

	public GetPronun(String word, String idioma){
		this.word = word;
		this.idioma = idioma;
		getForvo();
	}
	
	public void getForvo(){
		try {
			URL url = new URL("http://en.forvo.com/word/"+word+"/");

			Reader reader= new InputStreamReader((InputStream) url.getContent(), Charset.forName("UTF-8"));
			
			new ParserDelegator().parse(reader, this, true);
			
		} catch (IOException e) {
			log.error("No se puede acceder a forvo para obtener la pronunciacion", e);
		}
	}
	
	public void getWiki(){
		try {
			URL url = new URL("http://"+idioma+".wiktionary.org/wiki/"+word);

			Reader reader= new InputStreamReader((InputStream) url.getContent(), Charset.forName("UTF-8"));
			
			new ParserDelegator().parse(reader, this, true);
			
		} catch (IOException e) {
			log.error("Error al obtener la pronunciacion", e);
		}
	}
	
	@Override
	public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
		if( (t == HTML.Tag.DIV) || (t == HTML.Tag.SPAN) ){
			if((a.containsAttribute(HTML.Attribute.CLASS, "phonetic")) || 
					(a.containsAttribute(HTML.Attribute.CLASS, "ipa")) ||
					(a.containsAttribute(HTML.Attribute.CLASS, "IPA")) ||
					(a.containsAttribute(HTML.Attribute.CLASS, "API"))){
				String clase=a.getAttribute(HTML.Attribute.CLASS).toString();
				if( (clase.equals("phonetic") ||
						clase.equals("ipa") ||
						clase.equals("IPA") ||
						clase.equals("API") &&
						(!hasPhonetic) )){
					hasPhonetic=true;
					this.pos=pos;
				}
			}
		}
		else{
			hasPhonetic=false;
		}
	}
	
	@Override
	public void handleText(char[] data, int pos) {
		if( (hasPhonetic) && (phonetic==null) ){
			phonetic=String.valueOf(data);
		}
		
	}
	
	public String getPhonetic() {
		if(phonetic==null){
			getWiki();
		}
		return phonetic;
	}
	
	public  void setPhonetic(String phonetic) {
		this.phonetic = phonetic;
	}

	
	public static void main(String[] args) {
		
		String palabra = "Auto";
		String idioma = "de";
		String pronunciacion = new GetPronun(palabra, idioma).getPhonetic();
		try {
			log.info(new String(pronunciacion.getBytes(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			log.error("UnsupportedEncodingException al obtener la pronunciacion", e);
		}
	}
	
}
